package com.gf.event.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gf.base.controller.BasicController;
import com.gf.common.bean.DataRequest;
import com.gf.common.bean.DataResponse;
import com.gf.common.bean.Page;
import com.gf.common.constant.Constants;
import com.gf.common.constant.ViewPaths;
import com.gf.common.util.WebHelper;
import com.gf.event.service.EventService;
import com.gf.user.model.User;

/**
 * 用户网站动态事件
 * 
 * @author zhuguifeng
 * 
 */
@Controller
@RequestMapping("/event")
public class EventController extends BasicController {

	@Autowired
	private EventService eventService;
	
	private static Log log = LogFactory.getLog(EventController.class);

	/**
	 * 取得我关注人的所有动态 页面
	 */
	@RequestMapping(value = "/myConcerned", method = RequestMethod.GET)
	public ModelAndView myConcernedView() {
		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.MY_CONCERND_VIEW);

		return mvc;
	}

	/**
	 * 本人的动态 页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/myNews", method = RequestMethod.GET)
	public ModelAndView myNewsView() {
		ModelAndView mvc = new ModelAndView();

		mvc.setViewName(ViewPaths.MY_NEWS_VIEW);

		return mvc;
	}

	/**
	 * 得到所有用户的动态 页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/news", method = RequestMethod.GET)
	public ModelAndView newsView() {
		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.NEWS_VIEW);

		return mvc;
	}

	/**
	 * 浏览别的用户 主页显示的该用户的动态  页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/otherNews", method = RequestMethod.GET)
	public ModelAndView otherNewsView() {
		log.info("浏览别的用户：得到该用户动态");
		ModelAndView mvc = new ModelAndView();

		mvc.setViewName(ViewPaths.OTHER_NEWS_VIEW);

		return mvc;
	}

	/**
	 * 取得我关注人的所有动态
	 * 
	 * @return
	 */
	@RequestMapping(value = "/myConcernedData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse myConcernedData(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		long userId = user.getId();

		Page page = eventService.getEventsOfIdols(userId, curpage, pageSize);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}

	/**
	 * 本人的动态
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/myNewsData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse myNewsData(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		long userId = user.getId();

		Page page = eventService.getEventsByUserId(userId, curpage, pageSize);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}

	/**
	 * 得到所有用户的动态
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/newsData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse newsData(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		Page page = eventService.getAllEvents(curpage, pageSize);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}
	
	
	/**
	 * 浏览别的用户 主页显示的该用户的动态 数据
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/otherNewsData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse myNewsData(DataRequest dataRequest,long otherId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}
		
		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		Page page = eventService.getEventsByUserId(otherId, curpage, pageSize);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}

}
